﻿using Role_el.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Role_el.Controllers
{
    public class ChangController : ApiController
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetChangList()
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var talist = db.Askers.AsEnumerable();
                var count = talist.Count();
                var list = talist.Select(p => new
                {
                    AskerId = p.AskerId,
                    AskerName = p.AskerName,
                    RoleName = p.RoleName,
                    Weight = p.Weight,
                    BakContent = p.BakContent
                });
                return Json(new { count = count, data = list.OrderByDescending(p => p.Weight).ToList() });
            }
        }
        /// <summary>
        /// 修改权重
        /// </summary>
        /// <param name="ak"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult EditAk([FromBody] Askers ak)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                Askers aslist = db.Askers.SingleOrDefault(p => p.AskerId == ak.AskerId);
                aslist.Weight = ak.Weight;

                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new
                    {
                        code = 0,
                        msg = "修改成功！"
                    });
                }
                else
                {
                    return Json(new
                    {
                        code = -1,
                        msg = "修改失败！"
                    });
                }
            }
        }
    }
}
